package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;
import cn.com.api.service.*;

import cn.com.util.*;
import cn.com.util.*;


public class T_user_roleBaseController {

	private static Logger logger = Logger.getLogger(T_user_roleBaseController.class);
	
	@Autowired
	private ApiT_user_roleService apiT_user_roleService;
	@Autowired
	private ApiT_user_actionService apiT_user_actionService;
	@Autowired
	private ApiT_user_role_actionService apiT_user_role_actionService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "t_user_role-list")
	public ModelAndView t_user_role_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_user_role_list.jsp");
		
		List<ApiT_user_role> t_user_roles = apiT_user_roleService.findT_user_roles();
		for(ApiT_user_role _role : t_user_roles){
			List<ApiT_user_role_action> t_user_role_action_list = apiT_user_role_actionService.findT_user_role_actionsByRole_id(_role.getRole_id().toString());
			String _str_actions = "";
			for(ApiT_user_role_action action : t_user_role_action_list){
				ApiT_user_action user_action = apiT_user_actionService.findT_user_actionById(action.getAction_id());
				if(user_action!=null)
					_str_actions += " "+user_action.getRelevance();
			}
			_role.setAction_list(_str_actions);
		}
		mv.addObject("t_user_roles", t_user_roles);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_user_role-add-pre")
	public ModelAndView t_user_role_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_user_role_add.jsp");
		//权限代码集合
		List<ApiT_user_action> user_actions = new ArrayList<ApiT_user_action>();
		user_actions = apiT_user_actionService.findT_user_actions();
		
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_user_role t_user_role = apiT_user_roleService.findT_user_roleById(Integer.valueOf(id));
			mv.addObject("t_user_role",t_user_role);
			List<ApiT_user_role_action> t_user_role_action_list = apiT_user_role_actionService.findT_user_role_actionsByRole_id(id);
			for(ApiT_user_role_action action : t_user_role_action_list){
				for(ApiT_user_action user_action : user_actions){
					if(user_action.getAction_id()== action.getAction_id()){
						user_action.setIs_checked(true);
						break;
					}
				}
				//ApiT_user_action _action = apiT_user_actionService.findT_user_actionById(action.getAction_id());
			}
		}
		mv.addObject("actions",user_actions);

		
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_user_role-add")
	public @ResponseBody Map<String,Object> t_user_role_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String role_id = request.getParameter("role_id");
		String role_name = request.getParameter("role_name");
		String add_time = request.getParameter("add_time");
		String role_type = request.getParameter("role_type");
		String[] action_list = request.getParameterValues("action_list");

		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			if (Integer.parseInt(role_type) == 1){//新增为站长角色
				if(apiT_user_roleService.isHaveStationHeadRole()){
					map.put(Utils.RESULT, "-1");
					map.put(Utils.MESSAGE, "已有站长角色,不能重复添加！");
					return map;
				}
			}
			
			
			ApiT_user_role t_user_role = new ApiT_user_role();
            
		    //t_user_role.setRole_id(role_id);
		    t_user_role.setRole_name(role_name);
		    t_user_role.setRole_type(role_type);
		    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    t_user_role.setAdd_time(sdf.format(new Date()));
	      
			apiT_user_roleService.insertT_user_role(t_user_role);
			int ret_role_id = t_user_role.getRole_id();
			for(String action_id : action_list){
				ApiT_user_role_action role_action = new ApiT_user_role_action();
				role_action.setRole_id(ret_role_id);
				role_action.setAction_id(Integer.valueOf(action_id));
				apiT_user_role_actionService.insertT_user_role_action(role_action);
			}
		}
		//edit
		else{
			ApiT_user_role t_user_role = apiT_user_roleService.findT_user_roleById(Integer.valueOf(id));
			
			if (Integer.parseInt(role_type) == 1){//修改为站长角色
				if(apiT_user_roleService.isHaveStationHeadRole(Integer.parseInt(role_type))){
					map.put(Utils.RESULT, "-1");
					map.put(Utils.MESSAGE, "已有站长角色,此角色不能修改为站长角色！");
					return map;
				}
			}
			
		    //t_user_role.setRole_id(role_id);
		    t_user_role.setRole_name(role_name);
		    t_user_role.setRole_type(role_type);
		    //t_user_role.setAdd_time(add_time);
		    //更新权限代码
			List<ApiT_user_role_action> old_actions = apiT_user_role_actionService.findT_user_role_actionsByRole_id(id);
			for(ApiT_user_role_action old_action : old_actions){
				apiT_user_role_actionService.deleteT_user_role_action(old_action.getId());
			}
			for(String action_id : action_list){
				ApiT_user_role_action role_action = new ApiT_user_role_action();
				role_action.setRole_id(Integer.valueOf(id));
				role_action.setAction_id(Integer.valueOf(action_id));
				apiT_user_role_actionService.insertT_user_role_action(role_action);
			}
			apiT_user_roleService.updateT_user_role(t_user_role);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		return map;
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_user_role-delete")
	public void t_user_role_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_user_roleService.deleteT_user_role(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



